﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
//using System.Linq;
using System.Text;
using System.Windows.Forms;
//new
using System.IO;

namespace JKZP
{
    public partial class RoadForm : Form
    {
        public RoadForm()
        {
            InitializeComponent();
        }

        private void RoadForm_Load(object sender, EventArgs e)
        {
            dataGridView1.Rows.Clear();
            if (PP.gRoadList != null)
            {
                foreach (PP.RoadParam road in PP.gRoadList)
                {
                    dataGridView1.Rows.Add(new object[] { road.LKID, road.LKMC, road.GBDM, road.BMBH,road.USER });
                }
            }
        }

        private void button_add_Click(object sender, EventArgs e)
        {
            addroad form_addroad = new addroad();
            form_addroad.Owner = this;
            form_addroad.ShowDialog();
        }

        private void button_edit_Click(object sender, EventArgs e)
        {
            if (this.dataGridView1.SelectedRows.Count < 1)
            {
                MessageBox.Show("未选择要修改的记录");
            }
            else
            {
                editroad form_editroad = new editroad();
                form_editroad.Owner = this;
                form_editroad.rowindex = this.dataGridView1.SelectedRows[0].Index;
                form_editroad.roadid = this.dataGridView1.SelectedRows[0].Cells["roadid"].Value.ToString();
                form_editroad.roadname = this.dataGridView1.SelectedRows[0].Cells["roadname"].Value.ToString();
                form_editroad.gbdm = this.dataGridView1.SelectedRows[0].Cells["gbdm"].Value.ToString();
                form_editroad.bmbh = this.dataGridView1.SelectedRows[0].Cells["bmbh"].Value.ToString();
                form_editroad.user = this.dataGridView1.SelectedRows[0].Cells["user"].Value.ToString();
                form_editroad.ShowDialog();
            }
        }

        private void button_delete_Click(object sender, EventArgs e)
        {
            if (this.dataGridView1.SelectedRows.Count < 1)
            {
                MessageBox.Show("未选择要删除的记录");
            }
            else
            {
                if (DeleteRoad(this.dataGridView1.SelectedRows[0].Cells["roadid"].Value.ToString()) == true)
                {
                    this.dataGridView1.Rows.Remove(this.dataGridView1.SelectedRows[0]);
                    ((Form_Main)this.Owner).UpDateTree();
                    if (PP.gISWebVersion == false)
                    {
                        MessageBox.Show("删除成功");
                    }
                    else
                    {
                        if (PP.kdws.JKZP_UploadRoad(File.ReadAllText(PP.gRoadFile)) == true)
                        {
                            PP.ReLoad();
                            MessageBox.Show("删除成功");
                        }
                        else
                        {
                            MessageBox.Show("删除失败");
                        }
                    }
                }
                else
                {
                    MessageBox.Show("删除失败");
                }
            }
        }

        private void button_cancle_Click(object sender, EventArgs e)
        {
            Close();
        }

        private bool DeleteRoad(string roadid)
        {
            //<?xml version="1.0" encoding="UTF-8"?>
            //<param>
            //<road roadid="01" gbdm="123456789" roadname="测试路口一">
            //    <camera ip="172.16.22.252" type="HK9121" direction="由东向西" workmode="" bz=""></camera>
            //    <camera ip="172.16.22.250" type="HK9151" direction="由南向北" workmode="" bz=""></camera>
            //</road>
            //</param>
            string xpath = "descendant::road[@roadid='" + roadid + "']";
            bool ans = PP.DelConfig(PP.gRoadFile, xpath, false);
            if (ans == true)
            {
                PP.DelRoad(roadid);
            }
            return ans;
        }
    }
}
